$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL +'sys/publish/list',
        datatype: "json",
        colModel: [			
			{ label: 'rid', name: 'rid', index: 'rid', width: 50, key: true },
			{ label: '所属项目', name: 'project', index: 'project', width: 80 }, 			
			{ label: '所属模块', name: 'module', index: 'module', width: 80 },
            { label: '所属服务', name: 'serve', index: 'serve', width: 80 },
            { label: '更新描述', name: 'describe', index: 'describe', width: 80 },
			{ label: '发布环境', name: 'environment', index: 'environment', width: 80 },
			{ label: '发布时间', name: 'createTm', index: 'create_tm', width: 80 },
            { label: '发布人', name: 'createUser', index: 'create_user', width: 80 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        	// vm.dicList();
        }
    });
});

//js使用方式，指定语言、修改默认提示，添加自定义验证
var dict = {
    zh_CN:{
        messages:{
            required:function (name) {
                return name+"不能为空！";
            }
        },//定义成我们需要的文字提示
        attributes:{
            createUser:'发布人',
            describe:'更新描述',
            serve: '所属服务'
        },
    }
};
//引入vue后，需要添加vee-validata的js和语言包文件(目前只用中文)
VeeValidate.Validator.localize('zh_CN', dict.zh_CN);
Vue.use(VeeValidate);
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			tableName: null
		},
		title: null,
		publish: {},
        dicList:{},
        projectDicList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            this.errors.clear();
            $("#option_list").modal('show');
			vm.title = "新增";
            vm.publish = {project:"宁家鲜生",module:"trade",environment:"test"};
		},
		update: function (event) {
			var rid = getSelectedRow();
			if(rid == null){
				return ;
			}
            this.errors.clear();
            $("#option_list").modal('show');
            vm.title = "修改";
            
            vm.getInfo(rid)
           
		},
		saveOrUpdate: function (event) {
            this.$validator.validateAll().then(function(result) {
                if (result) {
                    var url = vm.publish.rid == null ? baseURL + "sys/publish/save" : baseURL + "sys/publish/update";
                    $.ajax({
                        type: "POST",
                        url: url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.publish),
                        success: function (r) {
                            if (r.code === 0) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
		},
		del: function (event) {
			var rids = getSelectedRows();
			if(rids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL +"sys/publish/delete",
				    contentType: "application/json",
				    data: JSON.stringify(rids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(rid){
			$.get(baseURL +"sys/publish/info/"+rid, function(r){
                vm.publish = r.publish;
            });
		},
		reload: function (event) {
            //$("#option_list").modal('hide');
            // vm.showList = true;
            $("#option_list").modal('hide');
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                //postData:{'roleName': vm.q.roleName},
                page:page
            }).trigger("reloadGrid");
		},
        getDicList: function(){
            $.get(baseURL + "sys/dic/select?code=MODULE", function(r){
                vm.dicList = r.list;
            });
        },
        getProjectDicList: function(){
            $.get(baseURL + "sys/dic/select?code=PROJECT", function(r){
                vm.projectDicList = r.list;
            });
        },

        changeType: function (ele) {
            var optionTxt = $(ele.target).find("option:selected").text();
            var optionVal = ele.target.value;
        },

    },
    mounted: function () {
	    this.getDicList();
	    this.getProjectDicList();
	}
});